import { marked } from 'marked';
import { escapeHTML } from './escape';
import { mangle } from 'marked-mangle';
import { setMdImgDom } from './setMdImgDom';
import { gfmHeadingId } from 'marked-gfm-heading-id';

// 转义文档中html语法
function fn(str: string) {
	const reg = /<\/?([a-z]+).*>/g;
	const ignoreTabList = ['img'];

	return str.replace(reg, (match, p1) => {
		if (!ignoreTabList.includes(p1)) {
			return escapeHTML(match);
		}

		return match;
	});
}

marked.use(mangle());
marked.use(
	gfmHeadingId({
		prefix: 'my-prefix-',
	})
);

export function generateMD(data: string) {
	return setMdImgDom(marked.parse(fn(data)));
}
